﻿using System;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace EpicBall.ScreenSystem
{
    public class LevelComplete : MenuScreen
    {
        private Texture2D _levelcomplete, _replayBtn, _nextBtn, _medal;
        private Vector2 pos;
        PhysicsGameScreen _screen;
        PhysicsGameScreen _nextScreen;
        private string highscore;

        public LevelComplete(PhysicsGameScreen screen, PhysicsGameScreen nextScreen)
            : base(798, 450)
        {

            TransitionOnTime = TimeSpan.FromSeconds(0.75);
            TransitionOffTime = TimeSpan.FromSeconds(0.75);
            TransitionAlpha = 0f;
            _screen = screen;
            _nextScreen = nextScreen;
            pos = new Vector2((_screen.GameStack.Dimensions.X - _screenWidth) / 2, (_screen.GameStack.Dimensions.Y - _screenHeight) /  2);
        }

        public override void LoadContent()
        {
            base.LoadContent();
            StreamReader sr = new StreamReader(_screen.GameStack.GetSavePath + "\\" + _screen.LevelTitle + ".hs");
            highscore = sr.ReadLine();
            sr.Close();

            //New highscore
            if (_screen.Score > Int32.Parse(highscore))
            {
                StreamWriter sw = new StreamWriter(_screen.GameStack.GetSavePath + "\\" + _screen.LevelTitle + ".hs");
                sw.WriteLine(_screen.Score.ToString());
                sw.Close();
            }
            mousePos = new Vector2(pos.X, pos.Y);
            _levelcomplete = _screen.GameStack.Content.Load<Texture2D>("LevelComplete/levelcomplete_bg");
            _replayBtn = _screen.GameStack.Content.Load<Texture2D>("LevelComplete/replaylvl_btn");
            _nextBtn = _screen.GameStack.Content.Load<Texture2D>("LevelComplete/nextlvl_btn");
            String medal="";
            if (_screen.Score >= _screen._levelGold) medal = "LevelComplete/gold_medal";
            else if (_screen.Score >= _screen._levelSilver) medal = "LevelComplete/silver_medal";
            else if (_screen.Score >= _screen._levelBronze) medal = "LevelComplete/bronze_medal";
 
            if(medal != "") _medal = _screen.GameStack.Content.Load<Texture2D>(medal);
            AddMenuButton(new MenuButton(this, _screen, _replayBtn, new Vector2(pos.X + 126, pos.Y + 366)));
            AddMenuButton(new MenuButton(this, _nextScreen, _nextBtn, new Vector2(pos.X + 242, pos.Y + 366)));
        }

        public override void Draw(GameTime gameTime)
        {
            _screen.GameStack.SpriteBatch.Begin();
            _screen.GameStack.SpriteBatch.Draw(_levelcomplete, new Rectangle((int)pos.X, (int)pos.Y, _screenWidth, _screenHeight), Color.White);
            if(_medal != null) _screen.GameStack.SpriteBatch.Draw(_medal, new Rectangle((int)(pos.X + 404), (int)(pos.Y+12), _medal.Width, _medal.Height), Color.White);
            DrawScore();
            _screen.GameStack.SpriteBatch.End();
            base.Draw(gameTime);
        }

        public void DrawScore()
        {
            String score = _screen.Score.ToString();
            SpriteFont font = _screen.GameStack.Content.Load<SpriteFont>("Fonts/Impact");
            Vector2 origin = font.MeasureString(score) / 2;
            _screen.GameStack.SpriteBatch.DrawString(font, highscore, new Vector2(pos.X + 268, pos.Y + 220), Color.White, 0, origin, 2, SpriteEffects.None, 1);
            _screen.GameStack.SpriteBatch.DrawString(font, score, new Vector2(pos.X + 268, pos.Y + 280), Color.White, 0, origin, 2, SpriteEffects.None, 1);
        }
    }
}
